<?php 
/* 	These code was generated using phpCIGen v 2.a (21/05/2012)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #CV. Trust Solution, jl. Saronojiwo 2/19 Surabaya, http://www.ts.co.id

	+ Module  		: m_div Model
	+ Description	: For record model process back-end
	+ Filename 		: c_m_div.php
 	+ creator 		: 
 	+ Created on 24/Jul/2012 11:40:14

*/

class M_m_div extends CI_Model{

		//constructor
		function __construct() {
			parent::__construct();
		}

		//function for get list record
		function m_div_list($filter,$start,$end){
			$sql = "SELECT div_id,
						   div_name,
						   div_active,
						   div_created_date,
						   div_created_by,
						   div_updated_date,
						   div_updated_by,
						   div_updated_count					FROM   m_div";

			// For simple search
			if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " ( lower(div_name) LIKE '%".addslashes(strtolower($filter))."%' )";
			}

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." LIMIT ".$start.", ".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function m_div_create($div_name, $div_active){
			$split_div_name = explode(" ", $div_name);
			$div_name = "";
			for($i=0; $i<sizeof($split_div_name); $i++){
				if($split_div_name[$i] != ""){
					if($i>0){
						$div_name .= " ";
					}
					$div_name .= $split_div_name[$i];
				}
			}
			
			//checking $div_name apakah sudah ada dalam database?
			//Jika "Ada" ==> return '0';
			//Jika "Tidak Ada" ==> return $this->db->insert_id();
			$sql = "SELECT div_id FROM m_div WHERE LOWER(div_name)='".strtolower($div_name)."'";
			$rs = $this->db->query($sql);
			if($rs->num_rows()>0){
				return '0';
			}
			
			$data = array(
					"div_name"=>$div_name,
					"div_active"=>$div_active,
					"div_created_by"=>$_SESSION[SESSION_USERID]
			);
			$this->db->insert('m_div', $data);
			if($this->db->affected_rows())
				return $this->db->insert_id();
			else
				return '0';
		}

		//function for update record
		function m_div_update($div_id,
				 $div_name,
				 $div_active,
				 $div_created_date,
				 $div_created_by,
				 $div_updated_date,
				 $div_updated_by,
				 $div_updated_count){
			$data = array(
						"div_name"=>$div_name,
						"div_active"=>$div_active,
						"div_created_date"=>$div_created_date,
						"div_created_by"=>$div_created_by,
						"div_updated_date"=>$div_updated_date,
						"div_updated_by"=>$div_updated_by,
						"div_updated_count"=>$div_updated_count					);

			$this->db->where('div_id', $div_id);
			$this->db->update('m_div', $data);
			return '1';
		}

		//fcuntion for delete record
		function m_div_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the m_divs at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM m_div WHERE div_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM m_div WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "div_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}

		//function for advanced search record
		function m_div_search($div_id,
				 $div_name,
				 $div_active,
				 $div_created_date,
				 $div_created_by,
				 $div_updated_date,
				 $div_updated_by,
				 $div_updated_count,$start,$end){
			//full query
			$sql = "SELECT div_id,
						   div_name,
						   div_active,
						   div_created_date,
						   div_created_by,
						   div_updated_date,
						   div_updated_by,
						   div_updated_count
					FROM   m_div";

			/* if($div_id!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(div_id) LIKE '%".addslashes(strtolower($div_id))."%'";
			}; */
			if($div_name!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(div_name) LIKE '%".addslashes(strtolower($div_name))."%'";
			};
			if($div_active!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(div_active) LIKE '%".addslashes(strtolower($div_active))."%'";
			};
			/* if($div_created_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(div_created_date) LIKE '%".addslashes(strtolower($div_created_date))."%'";
			};
			if($div_created_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(div_created_by) LIKE '%".addslashes(strtolower($div_created_by))."%'";
			};
			if($div_updated_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(div_updated_date) LIKE '%".addslashes(strtolower($div_updated_date))."%'";
			};
			if($div_updated_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(div_updated_by) LIKE '%".addslashes(strtolower($div_updated_by))."%'";
			};
			if($div_updated_count!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(div_updated_count) LIKE '%".addslashes(strtolower($div_updated_count))."%'";
			}; */

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function m_div_print($div_id,
				 $div_name,
				 $div_active,
				 $div_created_date,
				 $div_created_by,
				 $div_updated_date,
				 $div_updated_by,
				 $div_updated_count,$option,$filter){
			//full query
			$sql = "SELECT div_id,
						   div_name,
						   div_active,
						   div_created_date,
						   div_created_by,
						   div_updated_date,
						   div_updated_by,
						   div_updated_count
				    FROM   m_div";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(div_name) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				/* if($div_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_id) LIKE '%".addslashes(strtolower($div_id))."%'";
				}; */
				if($div_name!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_name) LIKE '%".addslashes(strtolower($div_name))."%'";
				};
				if($div_active!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_active) LIKE '%".addslashes(strtolower($div_active))."%'";
				};
				/* if($div_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_created_date) LIKE '%".addslashes(strtolower($div_created_date))."%'";
				};
				if($div_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_created_by) LIKE '%".addslashes(strtolower($div_created_by))."%'";
				};
				if($div_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_updated_date) LIKE '%".addslashes(strtolower($div_updated_date))."%'";
				};
				if($div_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_updated_by) LIKE '%".addslashes(strtolower($div_updated_by))."%'";
				};
				if($div_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_updated_count) LIKE '%".addslashes(strtolower($div_updated_count))."%'";
				}; */
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function m_div_export_excel($div_id ,$div_name ,$div_active ,$div_created_date ,$div_created_by ,$div_updated_date ,$div_updated_by ,$div_updated_count ,$option,$filter){
			//full query
			$sql = "SELECT div_id,
						   div_name,
						   div_active,
						   div_created_date,
						   div_created_by,
						   div_updated_date,
						   div_updated_by,
						   div_updated_count				    FROM   m_div";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= "(lower(div_name) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				/* if($div_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_id) LIKE '%".addslashes(strtolower($div_id))."%'";
				}; */
				if($div_name!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_name) LIKE '%".addslashes(strtolower($div_name))."%'";
				};
				if($div_active!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_active) LIKE '%".addslashes(strtolower($div_active))."%'";
				};
				/* if($div_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_created_date) LIKE '%".addslashes(strtolower($div_created_date))."%'";
				};
				if($div_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_created_by) LIKE '%".addslashes(strtolower($div_created_by))."%'";
				};
				if($div_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_updated_date) LIKE '%".addslashes(strtolower($div_updated_date))."%'";
				};
				if($div_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_updated_by) LIKE '%".addslashes(strtolower($div_updated_by))."%'";
				};
				if($div_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(div_updated_count) LIKE '%".addslashes(strtolower($div_updated_count))."%'";
				}; */
			}
			$query = $this->db->query($sql);
			return $sql;
		}

}
?>